import { createRouter, createWebHashHistory } from 'vue-router'
import Home from '../views/home.vue'
import My from '../views/my.vue'
import Password from '../views/password.vue'
import GestureVerify from '../views/gesture-verify.vue'
import Tb from '../views/tb.vue'
import Type from '../views/type.vue'
import addCategory from '../views/addCategory.vue'
import SkinStryle from '../views/skinStryle.vue'
import Content from '@/views/content.vue'
import Cart from '../views/add_cont.vue'
import Search from '@/views/search.vue'
import Invoice from '@/views/invoice.vue'
import Find from '@/views/find.vue'
import Find_add from '@/views/find_add.vue'

// requiresGesture 需要手势验证
// requiresAuth 需要登录

const router = createRouter({
    history: createWebHashHistory(),
    routes: [
        {
            path: '/',
            redirect: to => {
                const userInfo = JSON.parse(localStorage.getItem('userInfo') || '{}');
                const gesturePassword = localStorage.getItem('gesturePassword');
                const verified = sessionStorage.getItem('gestureVerified');
                
                // 如果用户已登录且设置了手势密码，但未验证
                if (userInfo.isLogin && gesturePassword && verified !== 'true') {
                    return '/gesture-verify';
                }
                return '/home';
            }
        },
        {
            path: '/home',
            name: 'home',
            component: Home,
            meta: {
                requiresGesture: true
            }
        },
        {
            path: '/category',
            name: 'tb',
            component: Tb,
            meta: {
                requiresGesture: true
            }
        },
        {
            path: '/my',
            name: 'my',
            component: My,
            meta: {
                requiresGesture: true
            }
        },
        {
            path: '/password',
            name: 'password',
            component: Password,
            meta: {
                requiresGesture: false,
                requiresAuth: true
            }
        },
        {
            path: '/gesture-verify',
            name: 'gesture-verify',
            component: GestureVerify,
            meta: {
                requiresGesture: false,
                requiresAuth: true
            }
        },
        {
            path: '/type',
            name: 'type',
            component: Type,
            meta: {
                requiresGesture: true,
                requiresAuth: true
            }
        },
        {
            path: '/add-category',
            name: 'addCategory',
            component: addCategory
        },
        {
            path: '/skinStryle',
            name: 'skinStryle',
            component: SkinStryle,
            meta: {
                requiresGesture: true,
                requiresAuth: true
            }
        },
        {
            path: '/content',
            name: 'content',
            component: Content,
            meta: {
                requiresGesture: true,
                requiresAuth: true
            }
        },
        {
            path: '/cart',
            name: 'cart',
            component: Cart,
            meta: {
                requiresGesture: true,
                requiresAuth: true
            }
        },
        {
            path: '/search',
            name: 'search',
            component: Search,
            meta: {
                requiresGesture: true,
                requiresAuth: true
            }
        },
        {
            path: '/invoice',
            name: 'invoice',
            component: Invoice,
            meta: {
                requiresGesture: true,
                requiresAuth: true
            }
        },
        {
            path: '/find',
            name: 'find',
            component: Find,
            meta: {
                requiresGesture: true,
                requiresAuth: true
            }
        },
        {
            path: '/find_add',
            name: 'find_add',
            component: Find_add,
            meta: {
                requiresGesture: true,
                requiresAuth: true
            }
        },
        {
            path: '/find_reply',
            name: 'FindReply',
            component: () => import('@/views/find_reply.vue')
        },
        {
            path: '/today',
            name: 'today',
            component: () => import('@/views/today.vue'),
            meta: {
                requiresGesture: true,
                requiresAuth: true
            }
        }
    ]
})

// 检查手势验证状态
const checkGestureVerification = () => {
    const verified = sessionStorage.getItem('gestureVerified');
    return verified === 'true';
};

// 清除验证状态
const clearVerification = () => {
    sessionStorage.removeItem('gestureVerified');
};

// 在应用启动时清除验证状态
clearVerification();

// 路由守卫
router.beforeEach((to, from, next) => {
    const userInfo = JSON.parse(localStorage.getItem('userInfo') || '{}');
    const gesturePassword = localStorage.getItem('gesturePassword');
    const isVerified = checkGestureVerification();
    
    // 检查是否需要登录
    if (to.meta.requiresAuth && !userInfo.isLogin) {
        // 如果需要登录但未登录，重定向到我的页面并传递显示登录框的参数
        next({ name: 'my', query: { showLogin: 'true' } });
        return;
    }
    
    // 如果用户未登录，只允许访问不需要登录的页面
    if (!userInfo.isLogin) {
        next();
        return;
    }
    
    // 如果用户已登录
    if (userInfo.isLogin) {
        // 如果设置了手势密码
        if (gesturePassword) {
            // 检查路由是否需要手势验证
            if (to.meta.requiresGesture) {
                // 如果未通过手势验证或验证状态不存在，重定向到验证页面
                if (!isVerified) {
                    // 如果当前已经在验证页面，允许访问
                    if (to.name === 'gesture-verify') {
                        next();
                    } else {
                        // 否则重定向到验证页面
                        next({ name: 'gesture-verify' });
                    }
                    return;
                }
            }
            
            // 如果已经通过手势验证，且尝试访问验证页面，重定向到首页
            if (isVerified && to.name === 'gesture-verify') {
                next({ name: 'home' });
                return;
            }
        }
    }
    
    // 其他情况，允许访问
    next();
});

export default router
